package com.ctshk.rpc.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.ctshk.common.constant.RedisConstants;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.goods.service.IESClassificationService;
import com.ctshk.rpc.order.goods.dto.CommodityClassificationAllLevelListDTO;
import com.ctshk.rpc.order.goods.dto.CommodityClassificationLevelTwoDTO;
import com.ctshk.rpc.order.goods.service.ICommodityClassificationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Slf4j
@DubboService
public class ESClassificationESServiceImpl implements IESClassificationService {

    @DubboReference
    private ICommodityClassificationService classificationService;

    private static String prefixStr = "ctshk:crm:";

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public Boolean updateClassificationES() {
        // 查询父子级分类名称集合列表
        List<CommodityClassificationAllLevelListDTO> classificationByAllLevel = classificationService.findClassificationByAllLevel(null);
        if (org.apache.dubbo.common.utils.CollectionUtils.isNotEmpty(classificationByAllLevel)) {
            log.info("【查询父子级分类名称集合列表，数据返参为:{}】", JSON.toJSONString(classificationByAllLevel));
            Collections.sort(classificationByAllLevel, new Comparator<CommodityClassificationAllLevelListDTO>() {
                @Override
                public int compare(CommodityClassificationAllLevelListDTO o1, CommodityClassificationAllLevelListDTO o2) {
                    if (null != o1.getSort() && null != o2.getSort()) {
                        if (o1.getSort() > o2.getSort()) {
                            return 1;
                        }
                        if (o1.getSort() == o2.getSort()) {
                            return 0;
                        }
                    }
                    return -1;
                }
            });
            List<CommodityClassificationLevelTwoDTO> dbLevelTwoData = new ArrayList<>();
            for (CommodityClassificationAllLevelListDTO levelListDTO : classificationByAllLevel) {
                if (org.apache.dubbo.common.utils.CollectionUtils.isNotEmpty(levelListDTO.getLevelTwo())) {
                    for (CommodityClassificationLevelTwoDTO levelTwoDTO : levelListDTO.getLevelTwo()) {
                        CommodityClassificationLevelTwoDTO levelTwo = EntityUtil.copy(levelTwoDTO, CommodityClassificationLevelTwoDTO.class);
                        // 0、不显示首页 1、显示首页
                        if (levelTwo != null && levelTwo.getIsDisplay() != null && levelTwo.getIsDisplay().intValue() == 1) {
                            if(levelTwo.getStatus() != null && levelTwo.getStatus().intValue() == 1){
                                dbLevelTwoData.add(levelTwo);
                            }
                        }
                    }
                }
            }
            // 上架redis
            String jsonString = JSON.toJSONString(dbLevelTwoData);
            redisTemplate.delete(RedisConstants.GOODS_HOME_CLASSIFICATION);
            redisTemplate.opsForValue().set(RedisConstants.GOODS_HOME_CLASSIFICATION, jsonString);

            String classificationByAllLevels = JSON.toJSONString(classificationByAllLevel);
            redisTemplate.delete(RedisConstants.GOODS_HOME_CLASSIFICATION_ALL);
            redisTemplate.opsForValue().set(RedisConstants.GOODS_HOME_CLASSIFICATION_ALL, classificationByAllLevels);
        } else {
            log.error("【查询父子级分类名称集合列表错误，数据为空】");
        }
        return true;
    }
}
